from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from .api.routes import router
from .utils.config import settings

# 创建FastAPI应用实例
def create_app():
    """创建FastAPI应用"""
    # 初始化应用
    app = FastAPI(
        title=settings.APP_NAME,
        description=settings.APP_DESCRIPTION
    )
    
    # 配置CORS
    app.add_middleware(
        CORSMiddleware,
        allow_origins=settings.ALLOWED_ORIGINS,
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )
    
    # 注册路由
    app.include_router(router)
    
    # 挂载静态文件目录，提供图像文件访问
    app.mount("/dicom-img", StaticFiles(directory=settings.DICOM_IMG_STORAGE), name="dicom_images")
    
    # 确保存储目录存在
    settings.ensure_directories()
    
    return app

# 创建应用实例
app = create_app()
